package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.DataFormatException;

@Service
@Slf4j
public class DishServicelmpl implements DishService {
    @Autowired
    private DishMapper dishMapper;


    /**
     * 根据分类id查询菜品
     *
     * @param id
     * @return
     */
    public List<Dish> getTypeDish(Integer id) {
        log.info("getTypeDish方法执行了");
        List<Dish> dish = new ArrayList<>();
        dish = dishMapper.getTypeDish(id);
        log.info(String.valueOf(dish));
        return dish;
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    public List searchDish(DishPageQueryDTO dishPageQueryDTO) {
        List<DishVO> DishList = new ArrayList<>();
        int start = (int) ((dishPageQueryDTO.getPage() - 1) * dishPageQueryDTO.getPageSize());
        DishList = dishMapper.searchDish(dishPageQueryDTO, start);
        log.info(DishList.toString());
        for (int i = 0; i < DishList.size(); i++) {
            DishList.get(i).setCategoryName(dishMapper.getCateName(DishList.get(i).getCategoryId()));
            ArrayList<DishFlavor> dishFlavors = new ArrayList<>();
            dishFlavors.addAll(dishMapper.getList(DishList.get(i).getId()));
            DishList.get(i).setFlavors(dishFlavors);
        }
        return DishList;
    }

    public long searchCount(DishPageQueryDTO dishPageQueryDTO) {
        log.info("searchCount方法执行了");
        Long count = dishMapper.getCount(dishPageQueryDTO);
        log.info("记录数{}", count);
        return count;
    }


    /**
     * 根据ID查询菜品
     *
     * @param id
     * @return
     */
    public DishVO getIdDish(Integer id) {
        DishVO dish = new DishVO();
        dish = dishMapper.getIdDish(id);
        log.info(dish.toString());
        dish.setFlavors(dishMapper.getList(Long.valueOf(id)));
        return dish;
    }


    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @Transactional
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        log.info(String.valueOf(dish));
        dishMapper.addDish(dish);
        Long dishId = dish.getId();
        int count = dishDTO.getFlavors().size();
        //可以使用SQL批量插入
        for (int i = 0; i < count; i++) {
            DishFlavor dishFlavor = new DishFlavor();
            dishFlavor.setDishId(dishId);
            dishFlavor.setName(dishDTO.getFlavors().get(i).getName());
            dishFlavor.setValue(dishDTO.getFlavors().get(i).getValue());
            dishMapper.addflavors(dishFlavor);
        }
    }


    /**
     * 启用状态设置
     *
     * @param status
     * @param id
     * @return
     */
    public void qi(short status, Long id) {
        dishMapper.qi(status, id);
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    //Todo 有关联套餐不能删
    @Transactional
    public void shan(Integer[] ids) {
        List<Integer> statusList = dishMapper.chaStatus(ids);
        for (int i = 0; i < statusList.size(); i++) {
            if (statusList.get(i) == 1) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        dishMapper.shan(ids);
        dishMapper.shanF(ids);
    }


    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @Transactional
    public void xiugai(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.xiugai(dish);
        dishMapper.xiugaishan(dishDTO);
        for (int i = 0; i < dishDTO.getFlavors().size(); i++) {
            dishMapper.xiugaiF(dishDTO.getFlavors().get(i), dishDTO.getId());
        }
    }
}
